<template>
  <div class="service-page">
    <!-- 背景组件 -->
    <ServiceBackground />
    
    <!-- 安全区域 -->
    <div class="safe-area">
      <!-- 顶部导航 -->
      <ServiceHeader />
    </div>
    
    <!-- 滚动容器 -->
    <div class="scroll-container">
      <!-- VIP服务卡片 -->
      <VipSection :vipServiceList="vipServiceList" />
      
      <!-- 家庭服务网格 -->
      <HomeServices :homeServiceList="homeServiceList" @service-click="handleServiceClick" />
      
      <!-- 服务日历 -->
      <ServiceCalendar :serviceActivity="serviceActivity" />
      
      <!-- 服务标签区域 -->
      <div class="service-tabs-section">
        <!-- 新人专区 -->
        <NewcomerZone :newcomerZone="newcomerZone" />
        
        <!-- 洁净家服务 -->
        <CleaningServices :cleaningServiceInfo="cleaningServiceInfo" @service-click="handleServiceClick" />
      </div>
    </div>

    <!-- 预约历史入口按钮 -->
    <div class="booking-history-btn" @click="goToBookingHistory">
      <el-icon><Calendar /></el-icon>
      <span>预约记录</span>
    </div>

    <!-- 预约服务弹窗 -->
    <ServiceBookingModal 
      :visible="bookingModalVisible" 
      :selectedService="selectedService"
      @close="handleCloseBookingModal"
      @submit="handleBookingSubmit"
    />
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { Calendar } from '@element-plus/icons-vue'
import ServiceBackground from './components/ServiceBackground.vue'
import ServiceHeader from './components/ServiceHeader.vue'
import VipSection from './components/VipSection.vue'
import HomeServices from './components/HomeServices.vue'
import ServiceCalendar from './components/ServiceCalendar.vue'
import NewcomerZone from './components/NewcomerZone.vue'
import CleaningServices from './components/CleaningServices.vue'
import ServiceBookingModal from './components/ServiceBookingModal.vue'
import { getServiceDataApi } from '@/api/service'

// 服务页面数据
const vipServiceList = ref([])
const homeServiceList = ref([])
const serviceActivity = ref({})
const newcomerZone = ref({})
const cleaningServiceInfo = ref([])

// 预约弹窗相关
const bookingModalVisible = ref(false)
const selectedService = ref({})

// 路由
const router = useRouter()

// 获取服务页面数据
const getServiceData = async () => {
  try {
    const serviceData = await getServiceDataApi()
    console.log('服务页面原始数据:', serviceData)
    
    // 检查数据结构并正确赋值
    vipServiceList.value = serviceData.vipServiceList || []
    homeServiceList.value = serviceData.homeServiceList || []
    
    // 处理 serviceActivity - 如果是数组，取第一个元素
    if (Array.isArray(serviceData.serviceActivity)) {
      serviceActivity.value = serviceData.serviceActivity[0] || {}
    } else {
      serviceActivity.value = serviceData.serviceActivity || {}
    }
    
    // 处理 newcomerZone - 如果是数组，取第一个元素
    if (Array.isArray(serviceData.newcomerZone)) {
      newcomerZone.value = serviceData.newcomerZone[0] || {}
    } else {
      newcomerZone.value = serviceData.newcomerZone || {}
    }
    
    cleaningServiceInfo.value = serviceData.cleaningServiceInfo || []
    
    // console.log('赋值后的数据:')
    // console.log('vipServiceList:', vipServiceList.value)
    // console.log('homeServiceList:', homeServiceList.value)
    // console.log('serviceActivity:', serviceActivity.value)
    // console.log('newcomerZone:', newcomerZone.value)
    // console.log('cleaningServiceInfo:', cleaningServiceInfo.value)
    
    // 检查具体字段
    if (serviceActivity.value) {
      console.log('serviceActivity 字段检查:')
      console.log('- name:', serviceActivity.value.name)
      console.log('- img:', serviceActivity.value.img)
      console.log('- title:', serviceActivity.value.title)
      console.log('- subTitle:', serviceActivity.value.subTitle)
    }
    
    if (newcomerZone.value) {
      console.log('newcomerZone 字段检查:')
      console.log('- img:', newcomerZone.value.img)
    }
  } catch (error) {
    console.error('获取服务页面数据失败:', error)
  }
}

// 处理服务点击事件
const handleServiceClick = (service) => {
  console.log('服务点击:', service)
  selectedService.value = service
  bookingModalVisible.value = true
}

// 关闭预约弹窗
const handleCloseBookingModal = () => {
  bookingModalVisible.value = false
  selectedService.value = {}
}

// 处理预约提交
const handleBookingSubmit = (bookingData) => {
  console.log('预约提交成功:', bookingData)
  // 预约提交成功后的处理逻辑
  // 可以在这里添加额外的业务逻辑，比如：
  // 1. 更新本地状态
  // 2. 发送通知
  // 3. 跳转到预约记录页面等
}

// 跳转到预约历史页面
const goToBookingHistory = () => {
  router.push('/service-booking-history')
}

onMounted(() => {
  getServiceData()
})
</script>

<style scoped>
.service-page {
  min-height: 100vh;
  position: relative;
  display: flex;
  flex-direction: column;
}

.safe-area {
  position: relative;
  z-index: 100;
  flex-shrink: 0;
}

.scroll-container {
  flex: 1;
  overflow-y: auto;
  position: relative;
  z-index: 10;
  padding-bottom: 20px;
  /* 为底部TabBar预留空间 */
  margin-bottom: 60px;
}

.service-tabs-section {
  /* 服务标签区域的容器样式 */
  position: relative;
}

/* 滚动条样式优化 */
.scroll-container::-webkit-scrollbar {
  width: 4px;
}

.scroll-container::-webkit-scrollbar-track {
  background: transparent;
}

.scroll-container::-webkit-scrollbar-thumb {
  background: rgba(0, 0, 0, 0.2);
  border-radius: 2px;
}

.scroll-container::-webkit-scrollbar-thumb:hover {
  background: rgba(0, 0, 0, 0.3);
}

/* 预约历史入口按钮 */
.booking-history-btn {
  position: fixed;
  bottom: 80px;
  right: 20px;
  background: linear-gradient(135deg, #409eff 0%, #337ab7 100%);
  color: white;
  padding: 12px 16px;
  border-radius: 25px;
  display: flex;
  align-items: center;
  gap: 8px;
  cursor: pointer;
  box-shadow: 0 4px 12px rgba(64, 158, 255, 0.3);
  transition: all 0.3s ease;
  z-index: 1000;
  font-size: 14px;
  font-weight: 500;
}

.booking-history-btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 6px 16px rgba(64, 158, 255, 0.4);
}

.booking-history-btn:active {
  transform: translateY(0);
}

/* 响应式设计 */
@media (max-width: 768px) {
  .scroll-container {
    margin-bottom: 50px;
  }
  
  .booking-history-btn {
    bottom: 70px;
    right: 16px;
    padding: 10px 14px;
    font-size: 13px;
  }
}
</style>
